﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RentHouse.Common;
using RentHouse.DTO;
using RentHouse.IService;
using RentHouse.Service.Entities;

namespace RentHouse.Service
{
    public class HouseAppointmentService : IHouseAppointmentService
    {
        public AjaxResult Add(HouseAppointmentAddDTO dto)
        {
            using (var db = new RentHouseContext())
            {
                var bs = new BaseService<T_HouseAppointments>(db);
                T_HouseAppointments appointments = new T_HouseAppointments
                {
                    VisitDate = dto.VisitDate,
                    CreateDateTime = DateTime.Now,
                    HouseId = dto.HouseId,
                    PhoneNum = dto.PhoneNum,
                    Status = "未处理",
                    Name = dto.Name,
                };
                if (bs.Add(appointments) > 0)
                {
                    return new AjaxResult(ResultState.Success, "您已成功提交看房预约，请等待业务员和您联系!");

                }
                else
                {
                    return new AjaxResult(ResultState.Error, "预约失败");

                }
            }
        }

        public List<HouseAppointmentDTO> GetHouseAppointmentsList (int start, int length, ref int rowCount)
        {
            using (var db=new RentHouseContext())
            {
                var bs = new BaseService<T_HouseAppointments>(db);
                var list=  bs.GetPagedList(start, length, ref rowCount, e => true, e => e.Id, true).Select(e => new HouseAppointmentDTO()
                {
                    Id = e.Id,
                    Name = e.Name,
                    PhoneNum = e.PhoneNum,
                    CreateDateTime = e.CreateDateTime,
                    VisitDate = e.VisitDate,
                    CommunityName = e.T_Houses.T_Communities.Name,
                    HouseAddress = e.T_Houses.T_Communities.Location,
                    Status = e.Status,
                    FollowAdminUserName = e.T_AdminUsers.Name
                }).ToList();
                return list;
            }
        }

        public AjaxResult Follow(int id,long adminUserId)
        {  
            


            using (var db = new RentHouseContext())
            {
                var bs = new BaseService<T_HouseAppointments>(db);
                //获取该订单信息
                var appointment = bs.Get(e => e.Id== id);
                //判断该订单是否存在
                if (appointment!=null)
                {    
                    //判断该订单是否已经被接单
                    if (appointment.FollowAdminUserId!= null)
                    {
                        if (appointment.FollowAdminUserId==adminUserId)
                            return new AjaxResult(ResultState.Error, "该订单已经被接单了");
                        else
                            return new AjaxResult(ResultState.Error, "该订单已经被他人抢走了");
                    }
                    else
                    {
                        appointment.FollowAdminUserId = adminUserId;
                        appointment.FollowDateTime = DateTime.Now;
                        appointment.Status = "已处理";

                        //利用数据库乐观锁进行并发处理（）
                        try
                        {
                            db.SaveChanges();
                            return new AjaxResult(ResultState.Success, "抢单成功");
                        }
                        catch (DbUpdateConcurrencyException)
                        {
                            return new AjaxResult(ResultState.Success, "该订单已被抢过了");

                        }

                    }
                }
                else
                {
                    return new AjaxResult(ResultState.Error, "该订单不存在");

                }



            }

        }
    }
}
